package cn.mrcode.mrweb.controller;

import cn.mrcode.smartFramework.annotation.Action;
import cn.mrcode.smartFramework.annotation.Controller;
import cn.mrcode.smartFramework.bean.Param;
import cn.mrcode.smartFramework.bean.View;
import cn.mrcode.smartPluginSecurity.annotation.User;
import cn.mrcode.smartPluginSecurity.exception.AuthcException;
import cn.mrcode.smartPluginSecurity.helper.SecurityHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 处理系统请求
 *
 * @author zhuqiang
 * @version V1.0
 * @date 2015/11/14 23:29
 */
@Controller
public class SystemController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SystemController.class);

    //进入首页
    @Action("/")
    public View index() {
        return new View("index.jsp");
    }

    //进入登录界面
    @Action("/login")
    public View login() {
        return new View("login.jsp");
    }

    /** 提交表单登录 **/
    @Action(value = "/login", method = "POST")
    public View loginSubmit(Param param) {
        String username = param.getString("username");
        String password = param.getString("password");
        try {
            SecurityHelper.login(username, password);
        } catch (AuthcException e) {
            LOGGER.error("login failure", e);
            return new View("login.jsp");
        }

        return new View("/customer");
    }

    /** 注销 **/
    @Action("/logout")
    @User
    public View logout() {
        SecurityHelper.logout();
        return new View("/");
    }
}
